package cn.amebius.simpledocker.controller.request;

import cn.amebius.simpledocker.exception.ParameterErrorException;
import lombok.Data;

/**
 * @author 阿伟
 * @date 2023/8/29 19:58
 */
@Data
public class AddDockerClientRequest {
    private static final String IP_ADDRESS_PATTERN =
            "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                    + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                    + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                    + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
    private static final String IP_PORT_PATTERN = "(?:[0-9]{1,4}|1[0-9]{4}|2[0-4][0-9]{3}|25[0-5]{2}|6553[0-5]|(?:655[0-2][0-9]|65535))";

    private String name;

    private String username;

    private String password;

    private String host;

    private String port;

    private String type;

    public void checkParameter() {
        if (name == null || host == null || port == null || type == null) {
            throw new ParameterErrorException("添加主机参数错误：参数丢失！");
        } else if (!host.matches(IP_ADDRESS_PATTERN)) {
            //Pattern.compile(IP_ADDRESS_PATTERN).matcher(host).matches()
            throw new ParameterErrorException("添加主机参数错误：主机IP格式错误！");
        } else if (!port.matches(IP_PORT_PATTERN)) {
            throw new ParameterErrorException("添加主机参数错误：主机端口格式错误！");
        }
    }
}
